// pages/find/dynamicDetail/dynamicDetail.js
let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dynamicId: null, //动态标识id
    allData: null, //整体信息
    sysMobileUser: null, //作者信息
    dynamicCommentList: null, //评论列表
    dynamicFileList: null, //图片列表


    showGuanzhu: true, //true  显示关注  false  显示取消关注
    followList: null,

    buttonType: 'default', //点赞按钮样式类别
    momentCount: 0, //文章点赞列表
    followList: null, //点赞列表
    buttonType: 'default', //点赞按钮样式类别
  },
  // GET/mobileUser/dynamic/getInfo/{dynamicId}获取动态详细信息
  getInfo(id) {
    wx.showLoading({
      title: '加载中...',
    })
    var postObj = {
      url: `/mobileUser/dynamic/getInfo/${id}`,
      method: 'get'
    }
    app.request.loginRequest(postObj).then(res => {
      wx.hideLoading()
      if (res.data.code === 200) {
        var data = res.data.data
        // 头像
        data.sysMobileUser.image = app.util.config().api + data.sysMobileUser.image
        // 视频
        if (!!data.videoUrl) {
          data.videoUrl = app.util.config().api + data.videoUrl
        }
        // 动态图片列表
        if (data.dynamicFileList.length > 0) {
          data.dynamicFileList.forEach((items, i) => {
            items.url = app.util.config().api + items.url
          })
        }
        //评论
        if (data.dynamicCommentList.length > 0) {
          data.dynamicCommentList.forEach((items, i) => {
            items.sysMobileUser.image = app.util.config().api + items.sysMobileUser.image
          })
        }


        this.setData({
          allData: data, //整体信息
          dynamicId: data.dynamicId,
          momentCount: res.data.data.pariseTotal,
          sysMobileUser: data.sysMobileUser, //作者信息
          dynamicCommentList: data.dynamicCommentList, //评论列表
          dynamicFileList: data.dynamicFileList, //图片列表
        })


        console.log('获取动态详情接口', data)



        if (app.globalData.hasLogin == true) {
          // this.checkcommentListZan()
          this.followList()
          // this.collectList()
        }

      }
    })

  

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);

    let that = this
    var dynamicId = options.dynamicId
    that.setData({
      dynamicId: dynamicId
    })
    // 获取文章信息
    that.getInfo(dynamicId)
    // 一进入页面检验点赞
    setTimeout(function () {
      if (app.globalData.hasLogin == true) {
        var postObj = {
          url: `/mobileUser/dynamicParise/list`,
          method: 'get',
          data: {
            dynamicId: dynamicId
          }
        }
        app.request.wxRequest(postObj).then(res => {
          if (res.data.code === 200) {
            console.log('检验是否点赞', res)
            if (res.data.rows.length == 0) {
              that.setData({
                buttonType: 'default'
              })
            } else {
              that.setData({
                buttonType: 'info'
              })
            }
          }
        })

      }

    }, 200)


    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    if (!!that.data.dynamicId) {
      that.getInfo(that.data.dynamicId)
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //  getCommentList   获取评论详情列表
  getCommentList(e) {
    var commentId = e.currentTarget.dataset.commentid
    var index = e.currentTarget.dataset.index
    console.log(index);
    
    var postObj = {
      url: `/mobileUser/dynamic/getComment/${commentId}`,
      method: 'get',
    }
    app.request.loginRequest(postObj).then(res => {
      if (res.data.code === 200) {
       
        var sysMomentCommentList = res.data.data
        var dynamicCommentList = this.data.dynamicCommentList
        dynamicCommentList[index].sysMomentCommentList = sysMomentCommentList
        this.setData({
          dynamicCommentList: dynamicCommentList
        })
        console.log('获取评论详情列表', dynamicCommentList)
      }
    })

  },
  //clearCommentList  收起评论详情
  clearCommentList(e) {
    var index = e.currentTarget.dataset.index
    var dynamicCommentList = this.data.dynamicCommentList

    delete dynamicCommentList[index].sysMomentCommentList //true

    this.setData({
      dynamicCommentList: dynamicCommentList
    })
  },


  //commentItem   回复一级评论信息  
  commentItem(e) {
    var commentId = e.currentTarget.dataset.commentid
    if (app.checkHasLogin()) {
      wx.navigateTo({
        url: '/pages/find/releaseNews/releaseNews?type=6&commentId=' + commentId,
      })
    }

  },


  // 检验文章是否点赞
  checkParise() {
    if (app.checkHasLogin()) {
      var postObj = {
        url: `/mobileUser/dynamicParise/list`,
        method: 'get',
        data: {
          dynamicId: this.data.dynamicId
        }
      }
      app.request.wxRequest(postObj).then(res => {
        if (res.data.code === 200) {
          console.log('检验是否点赞', res)
          if (res.data.rows.length == 0) {
            this.parise()
          } else {
            this.removeParise()
          }

        }
      })
    }
  },
  //文章点赞
  parise() {
    // app.checkHasLogin()
    if (app.checkHasLogin()) {
      var postObj = {
        url: `/mobileUser/dynamicParise`,
        method: 'post',
        data: {
          dynamicId: this.data.dynamicId
        }
      }
      app.request.wxRequest(postObj).then(res => {
        if (res.data.code === 200) {
          console.log('点赞', res)
          wx.showToast({
            title: '点赞成功',
            image: '../../../assets/success.png',
            duration: 1000,
            mask: true
          })
          this.setData({
            buttonType: 'info',
            momentCount: +this.data.momentCount + 1
          })

          var postObj = {
            url: `/mobileUser/dynamicParise/list`,
            method: 'get',
            data: {
              dynamicId: this.data.dynamicId
            }
          }
          app.request.wxRequest(postObj).then(res => {
            if (res.data.code === 200) {
              console.log('检验是否点赞', res)
              if (res.data.rows.length == 0) {
                this.setData({
                  buttonType: 'default'
                })
              } else {
                this.setData({
                  buttonType: 'info'
                })
              }
            }
          })

          // this.getRich(this.data.momentId)
        }
      })
    }
  },
  //文章 取消点赞
  removeParise() {
    var postObj = {
      url: `/mobileUser/dynamicParise/remove`,
      method: 'delete',
      data: {
        dynamicId: this.data.dynamicId
      }
    }
    app.request.wxRequest(postObj).then(res => {
      if (res.data.code === 200) {
        wx.showToast({
          title: '取消成功',
          image: '../../../assets/success.png',
          duration: 1000,
          mask: true
        })
        this.setData({
          buttonType: 'default',
          momentCount: (+this.data.momentCount - 1 <= 0) ? 0 : (+this.data.momentCount - 1)
        })

        var postObj = {
          url: `/mobileUser/dynamicParise/list`,
          method: 'get',
          data: {
            dynamicId: this.data.dynamicId
          }
        }
        app.request.wxRequest(postObj).then(res => {
          if (res.data.code === 200) {
            console.log('检验是否点赞', res)
            if (res.data.rows.length == 0) {
              this.setData({
                buttonType: 'default'
              })
            } else {
              this.setData({
                buttonType: 'info'
              })
            }
          }
        })


        // this.getRich(this.data.momentId)
      }
    })
  },



  //写评论  评论文章，不需要parentCommentId  type=1
  toWrite() {
    if (app.checkHasLogin()) {
      wx.navigateTo({
        url: '/pages/find/releaseNews/releaseNews?type=5&dynamicId=' + this.data.allData.dynamicId,
      })
    }
  },
  //去用户主页
  touserPage(e){
    console.log(e);
    
    if (app.checkHasLogin()) {
      wx.navigateTo({
        url: '/pages/find/userPage/userPage?mobileUserId=' + e.currentTarget.dataset.id,
      })
    }
  },
  // 分享给好友
  shareAppMessage() {
    // wx.showShareMenu({
    //   withShareTicket: false,
    //   menus: ['shareAppMessage', 'shareTimeline']
    // })
  },

  //获取关注列表
  followList() {
    var postObj = {
      url: `/mobileUser/follow/list`,
      method: 'get'
    }
    app.request.wxRequest(postObj).then(res => {
      if (res.data.code === 200) {
        var userId = this.data.sysMobileUser.mobileUserId
        console.log('获取关注列表', res)
        var followList = res.data.data
        for (let index = 0; index < followList.length; index++) {
          const element = followList[index];
          if (element.userId == userId) {
            this.setData({
              showGuanzhu: false
            })
            break;
          }
        }
        this.setData({
          followList: followList
        })
      }
    })
  },
  // 新增关注 POST/mobileUser/follow  
  followYes() {
    var userId = this.data.sysMobileUser.mobileUserId
    if (app.checkHasLogin()) {
      var postObj = {
        url: `/mobileUser/follow`,
        method: 'post',
        data: {
          userId: userId, //被关注人的id
        }
      }
      app.request.wxRequest(postObj).then(res => {
        if (res.data.code === 200) {
          console.log('新增关注', res)
          wx.showToast({
            title: '关注成功',
            image: '../../../assets/success.png',
            duration: 1000,
          })
          this.setData({
            showGuanzhu: false
          })
          this.followList()
        }
      })
    }

  },
  // 删除关注 /mobileUser/follow/edit删除关注
  followNo() {
    var userId = this.data.sysMobileUser.mobileUserId
    if (app.checkHasLogin()) {
      var postObj = {
        url: `/mobileUser/follow/edit`,
        method: 'put',
        data: {
          userId: userId
        }
      }
      app.request.wxRequest(postObj).then(res => {
        if (res.data.code === 200) {
          console.log('取消关注成功', res)
          wx.showToast({
            title: '取消关注',
            image: '../../../assets/success.png',
            duration: 1000,
          })
          this.setData({
            showGuanzhu: true
          })
          this.followList()
        }
      })
    }
  },

  //举报
  jubao() {
    wx.showLoading({
      title: '举报中...',
      success: () => {
        setTimeout(() => {
          setTimeout(() => {
            wx.hideLoading({
              success: (res) => {
                wx.showToast({
                  title: '举报成功',
                  image: '../../../assets/warning.png',
                  duration: 1000
                })
              },
            })
          }, 500)
        }, 500)
      }
    })

  },
  // 视频播放
  videoErrorCallback(e) {
    console.log('视频错误信息:')
    console.log(e.detail.errMsg)
  },
  bindVideoEnterPictureInPicture() {
    console.log('进入小窗模式')
  },
  bindVideoLeavePictureInPicture() {
    console.log('退出小窗模式')
  },



})